﻿using Mapster;
using System.Reflection;
using QiAdmin.Core;
using QiAdmin.Core.Services.Cache;
using QiAdmin.Core.Services.Token;
using QiAdmin.Core.Utils;
using QiAdmin.Model.Dtos.Input;
using QiAdmin.Model.Dtos.Output;
using QiAdmin.Service.IService;

namespace QiAdmin.WebApi
{
    public static class WebApplicationEx
    {
        /// <summary>
        /// 系统初始化
        /// </summary>
        /// <param name="app"></param>
        public static void Init(this WebApplication app)
        {
            //初始化下载路径
            if (!Directory.Exists(App.StaticData.UploadPath))
            {
                Directory.CreateDirectory(App.StaticData.UploadPath);
            }

            ICacheService cacheService = app.Services.GetService<ICacheService>();

            using (IServiceScope serviceScope = app.Services.CreateScope())
            {
                ITSysDictDataService dictDataService = serviceScope.ServiceProvider.GetService<ITSysDictDataService>();
                dictDataService.AddDictCache();
                Console.WriteLine("缓存字典数据");

                ITSysDeptService deptService = serviceScope.ServiceProvider.GetService<ITSysDeptService>();
                deptService.AddDeptCache();
                Console.WriteLine("缓存部门数据");

                ITSysRoleService roleService = serviceScope.ServiceProvider.GetService<ITSysRoleService>();
                roleService.AddRoleCache();
                Console.WriteLine("缓存角色数据");

                ITSysRoleMenuService roleMenuService = serviceScope.ServiceProvider.GetService<ITSysRoleMenuService>();
                roleMenuService.AddRoleMenuCache();
                Console.WriteLine("缓存角色菜单数据");


                var controllerNames = ControllerUtil.GetAllControllerNames(Assembly.GetExecutingAssembly());
                cacheService.Add("controllerData", controllerNames);
                Console.WriteLine("缓存控制器数据");

                ITSysTaskService taskService = serviceScope.ServiceProvider.GetService<ITSysTaskService>();
                taskService.Recover();
                Console.WriteLine("定时任务恢复");
            }

        }
    }
}
